package cn.wolfcode.rabc.service.impl;

import cn.wolfcode.rabc.domain.Department;
import cn.wolfcode.rabc.domain.Employee;
import cn.wolfcode.rabc.domain.Role;
import cn.wolfcode.rabc.mapper.EmployeeMapper;
import cn.wolfcode.rabc.qo.EmployeeQueryObject;
import cn.wolfcode.rabc.qo.QueryObject;
import cn.wolfcode.rabc.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements IEmployeeService {

    /**
     * 在类中定义一个静态 Logger 对象，传入当前类的作用是方便输出日志时可以清晰地看到该日志信息是属于哪个类的
     */
    //private static final Logger log = LoggerFactory.getLogger(EmployeeServiceImpl.class);

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void save(Employee employee) {
        log.info("新增为{}的数据", employee);
        employeeMapper.save(employee);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        log.info("删除为{}的数据", id);
        employeeMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        log.info("更新为{}的数据", employee);
        employeeMapper.update(employee);
    }

    @Override
    @Transactional
    public Employee get(Long id) {
        log.info("获取为{}的数据", id);
        return employeeMapper.get(id);
    }

    @Override
    @Transactional
    public List<Employee> listAll() {
        return employeeMapper.listAll();
    }


    public PageInfo<Employee> query(QueryObject qo) {
        // 使用分插件，当传入前页，每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> departments = employeeMapper.selectForList(qo);
        return new PageInfo(departments);
    }

    @Override
    public void deleteByEmployeeId(Long id) {
        employeeMapper.deleteByEmployeeId(id);
    }

    @Override
    public void insertRelation(Long roleId, Long EmployeeId) {
        employeeMapper.insertRelation(roleId, EmployeeId);
    }

    @Override
    public List<Role> queryByEmployeeId(Long id) {
        return employeeMapper.queryByEmployeeId(id);
    }

    @Override
    public Employee login(String username, String password) {
        return employeeMapper.login(username, password);
    }


}
